package com.xzk.ExpressInnWeb.controller.user;

import com.xzk.ExpressInnWeb.bean.*;
import com.xzk.ExpressInnWeb.service.UserService;
import com.xzk.ExpressInnWeb.util.JsonUtil;
import com.xzk.ExpressInnWeb.util.UserUtil;
import com.xzk.ExpressInnWeb.web.mvc.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

public class UserController {

    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        Map<String, Integer> values = UserService.consoleUser();
        Message msg = new Message();
        if (values.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);
        }
        msg.setData(values);
        return JsonUtil.toJSON(msg);
    }

    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        int offset = Integer.parseInt(req.getParameter("offset"));
        int limit = Integer.parseInt(req.getParameter("limit"));
        List<BootstrapTableUser> list = UserUtil.transferUserList(UserService.findAllUsers(true,offset,limit));
        int total = UserService.consoleUser().get("size");
        ResultData<BootstrapTableUser> data = new ResultData<>();
        data.setRows(list);
        data.setTotal(total);
        return JsonUtil.toJSON(data);
    }

    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
        Message msg = new Message();
        boolean insert = UserService.insertUser(new User(
                req.getParameter("username"),
                req.getParameter("personalid"),
                req.getParameter("userphone"),
                req.getParameter("password")
        ));
        if (insert)
            msg.setStatus(0);
        else
            msg.setStatus(-1);
        return JsonUtil.toJSON(msg);
    }

    @ResponseBody("/user/search.do")
    public String search(HttpServletRequest req, HttpServletResponse resp){
        User user = UserService.findUserByPhoneNum(req.getParameter("userphone"));
        Message msg = new Message();
        if (user==null) {
            msg.setStatus(-1);
            msg.setResult("手机号不存在");
        } else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(user);
        }
        return JsonUtil.toJSON(msg);
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        Message msg = new Message();
        boolean update = UserService.updateUser(id,new User(
                req.getParameter("username"),
                req.getParameter("personalid"),
                req.getParameter("userphone"),
                req.getParameter("password")
        ));
        if (update)
            msg.setStatus(0);
        else
            msg.setStatus(-1);
        return JsonUtil.toJSON(msg);
    }

    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        Message msg = new Message();
        boolean delete = UserService.deleteUser(id);
        if (delete) {
            msg.setStatus(0);
            msg.setResult("删除成功！");
        } else {
            msg.setStatus(-1);
            msg.setResult("删除失败！");
        }
        return JsonUtil.toJSON(msg);
    }

}
